package com.zheng.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.zheng.mall.dao.SmsCouponDao;
import com.zheng.mall.dao.SmsCouponProductCategoryRelationDao;
import com.zheng.mall.dao.SmsCouponProductRelationDao;
import com.zheng.mall.dto.SmsCouponParam;
import com.zheng.mall.mapper.SmsCouponMapper;
import com.zheng.mall.mapper.SmsCouponProductCategoryRelationMapper;
import com.zheng.mall.mapper.SmsCouponProductRelationMapper;
import com.zheng.mall.model.*;
import com.zheng.mall.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优惠券管理Service
 * @author zheng
 */
@Service
public class SmsCouponServiceImpl implements SmsCouponService {
    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private SmsCouponProductRelationMapper couponProductRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper couponProductCategoryRelationMapper;
    @Autowired
    private SmsCouponDao couponDao;
    @Autowired
    private SmsCouponProductRelationDao couponProductRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationDao couponProductCategoryRelationDao;

    /**
     * 添加优惠券
     *
     * @param couponParam
     */
    @Override
    public int create(SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        //插入优惠券和商品关系
        if (couponParam.getType().equals(2)) {
            for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                productRelation.setCouponId(couponParam.getId());
            }
            couponProductRelationDao.insertList(couponParam.getProductRelationList());
        }
        if (couponParam.getType().equals(1)) {
            for (SmsCouponProductCategoryRelation categoryRelation : couponParam.getProductCategoryRelationList()) {
                categoryRelation.setCouponId(couponParam.getId());
            }
            couponProductCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    /**
     * 根据优惠券id删除优惠券
     *
     * @param id
     */
    @Override
    public int delete(Long id) {
        int count = couponMapper.deleteByPrimaryKey(id);
        deleteProductRelation(id);
        deleteProductCategoryRelation(id);
        return count;
    }

    /**
     * 根据优惠券id更新优惠券信息
     *
     * @param id
     * @param couponParam
     */
    @Override
    public int update(Long id, SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        //插入优惠券和商品关系
        if (couponParam.getUseType().equals(2)) {
            for (SmsCouponProductRelation productRelation : couponParam.getProductRelationList()) {
                productRelation.setCouponId(couponParam.getId());
            }
            deleteProductRelation(id);
            couponProductRelationDao.insertList(couponParam.getProductRelationList());
        }
        if (couponParam.getUseType().equals(1)) {
            for (SmsCouponProductCategoryRelation categoryRelation : couponParam.getProductCategoryRelationList()) {
                categoryRelation.setCouponId(couponParam.getId());
            }
            deleteProductCategoryRelation(id);
            couponProductCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    /**
     * 分页获取优惠券列表
     *
     * @param name
     * @param type
     * @param pageSize
     * @param pageNum
     */
    @Override
    public List<SmsCoupon> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        new SmsCouponExample();
        return null;
    }

    /**
     * 获取优惠券详情
     *
     * @param id 优惠券表id
     */
    @Override
    public SmsCouponParam getItem(Long id) {
        return couponDao.getItem(id);
    }

    private void deleteProductCategoryRelation(Long id) {
        SmsCouponProductCategoryRelationExample productCategoryRelationExample = new SmsCouponProductCategoryRelationExample();
        productCategoryRelationExample.createCriteria().andCouponIdEqualTo(id);
        couponProductCategoryRelationMapper.deleteByExample(productCategoryRelationExample);
    }

    private void deleteProductRelation(Long id) {
        SmsCouponProductRelationExample productRelationExample = new SmsCouponProductRelationExample();
        productRelationExample.createCriteria().andCouponIdEqualTo(id);
        couponProductRelationMapper.deleteByExample(productRelationExample);
    }
}
